﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnimaGestionPersonnage.Utilities;
using AnimaGestionPersonnage.Properties;

namespace AnimaGestionPersonnage.Vue
{
    public partial class FormCreateTypicalModule : Form
    {
        private bool saved = true;
        private TypicalModule thisModule = new TypicalModule();

        public FormCreateTypicalModule()
        {
            InitializeComponent();
        }

        private void FormTypicalModule_Load(object sender, EventArgs e)
        {
            this.checkedListBoxWeaponList.Items.AddRange(Utils.weaponList.ToArray());
            this.thisModule.cost = 10;
        }

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            this.thisModule.name = this.textBoxName.Text;
            this.dataChanged();
        }

        private void numericUpDownCost_ValueChanged(object sender, EventArgs e)
        {
            this.thisModule.cost = (int)numericUpDownCost.Value;
            this.dataChanged();
        }

        private void checkedListBoxWeaponList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.thisModule.weaponList.Contains(Utils.weaponList[checkedListBoxWeaponList.SelectedIndex]))
            {
                this.thisModule.weaponList.Remove(Utils.weaponList[checkedListBoxWeaponList.SelectedIndex]);
            }
            else
            {
                this.thisModule.weaponList.Add(Utils.weaponList[checkedListBoxWeaponList.SelectedIndex]);
            }
            this.dataChanged();
        }

        private void richTextBoxDescription_TextChanged(object sender, EventArgs e)
        {
            this.thisModule.description = this.richTextBoxDescription.Text;
            this.dataChanged();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (Utils.weaponModuleList.Contains(this.thisModule))
            {
                Utils.weaponModuleList.Remove(this.thisModule);
            }
            Utils.weaponModuleList.Add(this.thisModule);
            Utils.weaponModuleList.Sort();
            Utils.xmlSerializeTypicalModules(Settings.Default.FilePath + Settings.Default.TypicalModule);
            this.saved = true;
            this.buttonSave.Enabled = false;
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            this.thisModule = new FormEditionSelection().ShowDialog(this);
            if (this.thisModule != null)
            {
                this.loadModule();
                this.saved = true;
                this.buttonSave.Enabled = false;
            }
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormCreateTypicalModule_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = Utils.onCloseEvent(saved, this, null,e);
        }

        #region local methods

        private void dataChanged()
        {
            if (Utils.getCurrentState() != Utils.LOADING_STATE)
            {
                this.buttonSave.Enabled = true;
                this.saved = false;
            }
        }

        private void loadModule()
        {
            int tempState = Utils.getCurrentState();
            int i = 0;

            Utils.setCurrentState(Utils.LOADING_STATE);

            this.numericUpDownCost.Value = this.thisModule.cost;
            this.richTextBoxDescription.Text =  this.thisModule.description;
            this.textBoxName.Text = this.thisModule.name;

            foreach (int ci in this.checkedListBoxWeaponList.CheckedIndices)
            {
                this.checkedListBoxWeaponList.SetItemChecked(ci, false);
            }
            foreach (Weapon wep in this.thisModule.weaponList)
            {
                foreach (Weapon wepLis in Utils.weaponList)
                {
                    if (wepLis.Equals(wep))
                    {
                        this.checkedListBoxWeaponList.SetItemChecked(i, true);
                        i = 0;
                        break;
                    }
                    i++;
                }
            }

            Utils.setCurrentState(tempState);
        }

        #endregion
    }
}
